package com.ruoyi.pg.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.pg.domain.Indicator;
import com.ruoyi.pg.domain.PgTreeSelect;
import com.ruoyi.pg.mapper.IndicatorMapper;
import com.ruoyi.pg.service.IIndicatorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 指标点Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-07
 */
@Service
public class IndicatorServiceImpl implements IIndicatorService 
{
    @Resource
    private IndicatorMapper indicatorMapper;

    /**
     * 查询指标点
     * 
     * @param indicatorId 指标点主键
     * @return 指标点
     */
    @Override
    public Indicator selectIndicatorByIndicatorId(Long indicatorId)
    {
        return indicatorMapper.selectIndicatorByIndicatorId(indicatorId);
    }

    /**
     * 查询指标点列表
     * 
     * @param indicator 指标点
     * @return 指标点
     */
    @Override
    public List<Indicator> selectIndicatorList(Indicator indicator)
    {
        return indicatorMapper.selectIndicatorList(indicator);
    }

    /**
     * 新增指标点
     * 
     * @param indicator 指标点
     * @return 结果
     */
    @Override
    public int insertIndicator(Indicator indicator)
    {
        indicator.setCreateTime(DateUtils.getNowDate());
        return indicatorMapper.insertIndicator(indicator);
    }

    /**
     * 修改指标点
     * 
     * @param indicator 指标点
     * @return 结果
     */
    @Override
    public int updateIndicator(Indicator indicator)
    {
        indicator.setUpdateTime(DateUtils.getNowDate());
        return indicatorMapper.updateIndicator(indicator);
    }

    /**
     * 批量删除指标点
     * 
     * @param indicatorIds 需要删除的指标点主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorByIndicatorIds(Long[] indicatorIds)
    {
        // 1. 检查是否有下级指标点
        for (Long indicatorId : indicatorIds) {
            int childCount = indicatorMapper.selectChildIndicatorCount(indicatorId);
            if (childCount > 0) {
                return -1;
            }
        }

        // 2. 执行批量删除
        return indicatorMapper.deleteIndicatorByIndicatorIds(indicatorIds);
    }

    /**
     * 删除指标点信息
     * 
     * @param indicatorId 指标点主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorByIndicatorId(Long indicatorId)
    {
        return indicatorMapper.deleteIndicatorByIndicatorId(indicatorId);
    }

    /**
     * 查询指标树结构信息
     *
     * @param indicator 指标信息
     * @return 指标树信息集合
     */
    @Override
    public List<PgTreeSelect> selectIndicatorTreeList(Indicator indicator)
    {
        List<Indicator> indicators = SpringUtils.getAopProxy(this).selectIndicatorList(indicator);
        return buildIndicatorTreeSelect(indicators);
    }

    /**
     * 构建前端所需要下拉指标结构
     *
     * @param indicators 指标列表
     * @return 下拉树结构列表
     */
    @Override
    public List<PgTreeSelect> buildIndicatorTreeSelect(List<Indicator> indicators)
    {
        List<Indicator> indicatorTrees = buildIndicatorTree(indicators);
        //return indicatorTrees.stream().map(PgTreeSelect::new).collect(Collectors.toList());
        // 确保顶层节点的id不为null
        return indicatorTrees.stream()
                .map(indicator -> {
                    PgTreeSelect treeSelect = new PgTreeSelect(indicator);
                    if (treeSelect.getId() == null) {
                        treeSelect.setId(0L); // 设置顶层节点id为0
                    }
                    return treeSelect;
                })
                .collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param indicators 指标列表
     * @return 树结构列表
     */
    @Override
    public List<Indicator> buildIndicatorTree(List<Indicator> indicators)
    {
        List<Indicator> returnList = new ArrayList<Indicator>();
        List<Long> tempList = indicators.stream().map(Indicator::getIndicatorId).collect(Collectors.toList());
        for (Indicator indicator : indicators)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(indicator.getParentId()))
            {
                recursionFn(indicators, indicator);
                returnList.add(indicator);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = indicators;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<Indicator> list, Indicator t)
    {
        // 得到子节点列表
        List<Indicator> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Indicator tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Indicator> getChildList(List<Indicator> list, Indicator t)
    {
        List<Indicator> tlist = new ArrayList<Indicator>();
        Iterator<Indicator> it = list.iterator();
        while (it.hasNext())
        {
            Indicator n = (Indicator) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getIndicatorId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Indicator> list, Indicator t)
    {
        return getChildList(list, t).size() > 0;
    }
}
